package cn.wolfcode.producer._02_send_type;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.CountDownLatch;

public class Producer1 {
    public static void main(String[] args) throws Exception {
        //1.创建生产者对象
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("producer-demo");
        //2.设置名字服务器地址
        defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动生产者
        defaultMQProducer.start();
        CountDownLatch count = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            //4.创建消费并发送消息
            Message message = new Message("02-send-type","你好RocketMQ".getBytes());
            defaultMQProducer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println("消息发送成功");
                    count.countDown();
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println("消息发送失败");
                    count.countDown();
                }
            });
        }
        count.await();
//        System.out.println(sendResult.getMsgId());
//        System.out.println(sendResult.getSendStatus());
        //5.关闭资源
        defaultMQProducer.shutdown();
    }
}
